package com.itcc.service.impl;

import com.itcc.controller.Code;
import com.itcc.dao.BookDao;
import com.itcc.domain.Book;
import com.itcc.exception.BusinessException;
import com.itcc.exception.SystemException;
import com.itcc.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookDao bookDao;

    public boolean insert(Book book) {
        return bookDao.insert(book) > 0;
    }

    public boolean delete(Integer id) {
        if(id == 1 || id == 2) {
            throw new BusinessException(Code.BUSINESS_ERR, "请不要使用你的技术挑战我的耐性！");
        }
        return bookDao.delete(id) > 0;
    }

    public boolean update(Book book) {
        return bookDao.update(book) > 0;
    }

    public Book findById(Integer id) {
        if(id == 1 || id == 2) {
            throw new BusinessException(Code.BUSINESS_ERR, "请不要使用你的技术挑战我的耐性！");
        }
        //将可能出现的异常进行包装，转换成自定义的异常
        try {
//            int j = 1/0;    //模拟异常
            return bookDao.findById(id);
        }catch (Exception e){
            throw new SystemException(Code.SYSTEM_TIMEOUT_ERR, "服务器访问超时，请重试！", e);
        }

    }

    public List<Book> selectAll() {
        return bookDao.selectAll();
    }

    public List<Book> selectOther(String name) {
        return bookDao.selectOther(name);
    }

}
